package com.mdd.front.service.impl.zdjd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.zdjd.TZdjdDeviceData;
import com.mdd.common.mapper.zdjd.TZdjdDeviceDataMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.zdjd.ITZdjdDeviceDataService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceDataCreateValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceDataSearchValidate;
import com.mdd.front.validate.zdjd.TZdjdDeviceDataUpdateValidate;
import com.mdd.front.vo.zdjd.TZdjdDeviceDataDetailVo;
import com.mdd.front.vo.zdjd.TZdjdDeviceDataListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 设备数据回传记录实现类
 * @author LikeAdmin
 */
@Service
public class TZdjdDeviceDataServiceImpl implements ITZdjdDeviceDataService {
        
    @Resource
    TZdjdDeviceDataMapper tZdjdDeviceDataMapper;

    /**
     * 设备数据回传记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TZdjdDeviceDataListedVo>
     */
    @Override
    public PageResult<TZdjdDeviceDataListedVo> list(PageValidate pageValidate, TZdjdDeviceDataSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TZdjdDeviceData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tZdjdDeviceDataMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:deviceId@device_id:long",
            "=:energyConsumption@energy_consumption:str",
            "=:runTime@run_time:long",
            "=:year:str",
            "=:month:str",
            "=:day:str",
        });

        IPage<TZdjdDeviceData> iPage = tZdjdDeviceDataMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TZdjdDeviceDataListedVo> list = new LinkedList<>();
        for(TZdjdDeviceData item : iPage.getRecords()) {
            TZdjdDeviceDataListedVo vo = new TZdjdDeviceDataListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 设备数据回传记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TZdjdDeviceData
     */
    @Override
    public TZdjdDeviceDataDetailVo detail(Integer id) {
        TZdjdDeviceData model = tZdjdDeviceDataMapper.selectOne(
                new QueryWrapper<TZdjdDeviceData>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TZdjdDeviceDataDetailVo vo = new TZdjdDeviceDataDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 设备数据回传记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TZdjdDeviceDataCreateValidate createValidate) {
        TZdjdDeviceData model = new TZdjdDeviceData();
        model.setDeviceId(createValidate.getDeviceId());
        model.setEnergyConsumption(createValidate.getEnergyConsumption());
        model.setRunTime(createValidate.getRunTime());
        model.setCreateTime(new Date());
        model.setYear(createValidate.getYear());
        model.setMonth(createValidate.getMonth());
        model.setDay(createValidate.getDay());
        model.setUpdateTime(new Date());
        tZdjdDeviceDataMapper.insert(model);
    }

    /**
     * 设备数据回传记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TZdjdDeviceDataUpdateValidate updateValidate) {
        TZdjdDeviceData model = tZdjdDeviceDataMapper.selectOne(
                new QueryWrapper<TZdjdDeviceData>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setDeviceId(updateValidate.getDeviceId());
        model.setEnergyConsumption(updateValidate.getEnergyConsumption());
        model.setRunTime(updateValidate.getRunTime());
        model.setYear(updateValidate.getYear());
        model.setMonth(updateValidate.getMonth());
        model.setDay(updateValidate.getDay());
        model.setUpdateTime(new Date());
        tZdjdDeviceDataMapper.updateById(model);
    }

    /**
     * 设备数据回传记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TZdjdDeviceData model = tZdjdDeviceDataMapper.selectOne(
                new QueryWrapper<TZdjdDeviceData>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tZdjdDeviceDataMapper.delete(new QueryWrapper<TZdjdDeviceData>().eq("id", id));
    }

    /**
     * 设备数据回传记录新增
     * @param deviceData 设备数据回传记录实体
     */
    @Override
    public void insertDeviceData(TZdjdDeviceData deviceData) {
        tZdjdDeviceDataMapper.insert(deviceData);
    }

}
